package com.liyiyan.note;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.liyiyan.note.R;
import com.liyiyan.note.util.GetHtmlService;
import com.liyiyan.note.util.HelperTool;



public class NoteEditActivity extends Activity {
	protected static final String TAG = "NoteEditActivity";

	private Handler getHandler(){
		if (handler == null){
			handler = new Handler(Looper.getMainLooper()) {
				public void handleMessage(Message msg) {
					String contentString = msg.getData().getString("result");
					Log.i(TAG, contentString);
					if (contentString.compareTo("true")==0){
						HelperTool.showMessage(NoteEditActivity.this, "Note Submitted");
						EditText titleText = (EditText)findViewById(R.id.titleText);
						EditText contentText = (EditText)findViewById(R.id.contentText);
						titleText.setText("");
						contentText.setText("");
					} else if (contentString.compareTo("true")==0){
						HelperTool.showMessage(NoteEditActivity.this, "Failed. Try Again.");
					}
				}
			};
		}
		return handler;
	}
	
	private int userID = 1; //for testing purpose
	//private NoteBean noteBean;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.insert);
		Button submitButton = (Button)findViewById(R.id.submitButton);
		submitButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				EditText titleText = (EditText)findViewById(R.id.titleText);
				EditText contentText = (EditText)findViewById(R.id.contentText);
				String titleString = titleText.getText().toString();
				String contentString = contentText.getText().toString();
				String userIdString = ""+ userID;
				Resources res = NoteEditActivity.this.getResources();
				if (titleString.length()==0){
					titleText.requestFocus();
					HelperTool.showMessage(NoteEditActivity.this, res.getString(R.string.emptytitle));
					return;
				}
				
				if (contentString.length()==0){
					contentText.requestFocus();
					HelperTool.showMessage(NoteEditActivity.this, res.getString(R.string.emptycontent));
					return;
				}
				
				List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
				nameValuePairs.add(new BasicNameValuePair("pid",  userIdString));
				nameValuePairs.add(new BasicNameValuePair("title", URLEncoder.encode(titleString)));
				nameValuePairs.add(new BasicNameValuePair("note",  URLEncoder.encode(contentString)));
				Handler handler = getHandler();
				new GetHtmlService(NoteEditActivity.this, handler,GetHtmlService.INSERT, nameValuePairs).start();
			}
		});


	}
	

	private static Handler handler;
}